package Server;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.Timer;

import java.awt.Component;
import javax.swing.SwingConstants;

import Game.Challange;
import Game.Watch;
import GameUI.PictureUI;
import GameUI.Uihealthbar;

import java.awt.Label;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Color;
/**
 * This is the ui of the client include the some process of the game.
 * @author wat wattanagaroon
 * @author Parinthorn Panya
 *
 */
public class ClientUI extends JFrame implements Observer {

	private Uihealthbar barUi;
	private Client client;
	private JTextField messagefield;
	private JButton enter;
	private JButton play;
	private JLabel show;
	private Container contain;
	private Label label;
	private int problem;
	private Challange ch;
	private String[] from;
	private ImageIcon[] images;
	private Timer time;
	/**
	 * The clientui constructor.
	 * @param client
	 */
	public ClientUI(Client client) {
		super("TYPING FIGHT!!!!");
		time = new Timer(1700, new timepass());
		time.setRepeats(false);
		setBackground(new Color(0, 0, 128));
		getContentPane().setBackground(new Color(255, 99, 71));
		this.client = client;
		initComponent();
		client.addObserver(this);
		setLocation(512, 333);
		run();
		setDefaultCloseOperation(EXIT_ON_CLOSE);

	}
	/**
	 * The inticomponent of the clientui
	 */
	public void initComponent() {
		messagefield = new JTextField();
		messagefield.setFont(new Font("Tahoma", Font.PLAIN, 16));
		messagefield.setPreferredSize(new Dimension(100, 30));
		enter = new JButton("ENTER!");
		enter.setFont(new Font("Tahoma", Font.BOLD, 12));
		enter.setForeground(new Color(60, 179, 113));
		enter.setPreferredSize(new Dimension(80, 30));
		show = new JLabel();
		show.setFont(new Font("Tahoma", Font.BOLD, 16));
		show.setForeground(new Color(230, 230, 250));
		show.setHorizontalAlignment(SwingConstants.CENTER);
		enter.addActionListener(new enterAction());
		show.setPreferredSize(new Dimension(200, 30));

		contain = new Container();
		contain.setLayout(new FlowLayout());

		label = new Label("Name :");
		label.setFont(new Font("Dialog", Font.BOLD, 17));
		contain.add(label);
		contain.add(messagefield);
		contain.add(enter);

		getContentPane().add(contain, BorderLayout.CENTER);
		getContentPane().add(show, BorderLayout.SOUTH);

	}
	/**
	 * Run method
	 */
	public void run() {
		pack();
		setVisible(true);
	}
	/**
	 * The update method after get the notify
	 */
	@Override
	public void update(Observable arg0, Object arg1) {
		show.setText(arg1.toString());
		if (arg1.toString().startsWith("p")) {
			show.setText("Press Button Play");
			from = arg1.toString().split("/");
			problem = Integer.parseInt(from[1]);
			toPlay();

		} else if (arg1.toString().startsWith("b")) {
			problem = Integer.parseInt(arg1.toString().substring(1));
			barUi.watch.timeAgain();
			ch.setNewProblem(problem);
			barUi.inputfield.setText(ch.sendWord());
			barUi.outputfield.setText("");
		} else if (show.getText().toLowerCase().contains("out")) {
			play.setVisible(false);
			enter.setVisible(true);
		}

		else if (arg1 instanceof Integer) {
			barUi.picui.run();
			images = barUi.picui.getImages();
			if (((Integer) arg1).intValue() == 2) {
				barUi.bar1.setValue(barUi.bar1.getValue() - 10);
				barUi.picui.changePicture(images[1]);
				barUi.add(barUi.picui.panelpicturecenter, BorderLayout.CENTER);
				time.start();
			} else if (((Integer) arg1).intValue() == 1) {
				barUi.bar2.setValue(barUi.bar2.getValue() - 10);
				barUi.picui.changePicture(images[2]);
				// barUi.picui.changePicture(barUi.picui.rightatk,barUi.picui.picturestandard);
				// barUi.picui.standardpicture(barUi.picui.leftatk);
				barUi.add(barUi.picui.panelpicturecenter, BorderLayout.CENTER);
				time.start();
			} else
				barUi.inputfield.setText(ch.sendWord());
		} else {
		}

	}
	/**
	 * The enter action of the subclass
	 *
	 */
	class enterAction extends AbstractAction {

		@Override
		public void actionPerformed(ActionEvent arg0) {

			messagefield.setEditable(false);
			enter.setEnabled(false);
			if (messagefield.getText() != null) {
				try {
					client.sendToServer(messagefield.getText());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
	/**
	 * The subclass of the play action.
	 */
	class playAction extends AbstractAction {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			play.setVisible(false);
			show.setVisible(false);
			messagefield.setVisible(false);
			ch = new Challange(client, problem);
			barUi = new Uihealthbar(new Watch(), ch);
			barUi.player1name.setText(from[2]);
			barUi.setName1(from[2]);
			barUi.player2name.setText(from[3]);
			barUi.setName2(from[3]);
			setVisible(false);
		}

	}
	/**
	 * The method that play the game.
	 */
	public void toPlay() {
		enter.setVisible(false);
		play = new JButton("PLAY");
		contain.add(play);
		play.addActionListener(new playAction());
	}
	/**
	 * The subclass that show timepass
	 *
	 */
	class timepass implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			barUi.picui.standardpicture();
			barUi.add(barUi.picui.panelpicturecenter, BorderLayout.CENTER);

		}

	}
}
